package cn.seocoo.platform.web.controller.rule;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.seocoo.platform.common.annotation.Log;
import cn.seocoo.platform.common.enums.BusinessType;
import cn.seocoo.platform.rule.domain.Rule;
import cn.seocoo.platform.rule.service.IRuleService;
import cn.seocoo.platform.common.core.controller.BaseController;
import cn.seocoo.platform.common.core.page.TableDataInfo;
import cn.seocoo.platform.common.core.domain.AjaxResult;
import cn.seocoo.platform.common.utils.poi.ExcelUtil;

/**
 * 规则设置 信息操作处理
 * 
 * @author wujian
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/rule/rule")
public class RuleController extends BaseController
{
    private String prefix = "rule/rule";
	
	@Autowired
	private IRuleService ruleService;
	
	@RequiresPermissions("rule:rule:view")
	@GetMapping()
	public String rule()
	{
	    return prefix + "/rule";
	}
	
	/**
	 * 查询规则设置列表
	 */
	@RequiresPermissions("rule:rule:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Rule rule)
	{
		startPage();
        List<Rule> list = ruleService.selectRuleList(rule);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出规则设置列表
	 */
	@RequiresPermissions("rule:rule:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Rule rule)
    {
    	List<Rule> list = ruleService.selectRuleList(rule);
        ExcelUtil<Rule> util = new ExcelUtil<Rule>(Rule.class);
        return util.exportExcel(list, "rule");
    }
	
	/**
	 * 新增规则设置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存规则设置
	 */
	@RequiresPermissions("rule:rule:add")
	@Log(title = "规则设置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Rule rule)
	{		
		return toAjax(ruleService.insertRule(rule));
	}

	/**
	 * 修改规则设置
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Rule rule = ruleService.selectRuleById(id);
		mmap.put("rule", rule);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存规则设置
	 */
	@RequiresPermissions("rule:rule:edit")
	@Log(title = "规则设置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Rule rule)
	{		
		return toAjax(ruleService.updateRule(rule));
	}
	
	/**
	 * 删除规则设置
	 */
	@RequiresPermissions("rule:rule:remove")
	@Log(title = "规则设置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(ruleService.deleteRuleByIds(ids));
	}
	
}
